package com.joven.security.service.impl;

import com.alibaba.fastjson.JSON;
import com.joven.security.utils.ResultData;
import com.joven.security.utils.WebUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author wzj
 * @Date 2023-03-01 15:35
 **/
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        ResultData resultData = new ResultData(HttpStatus.UNAUTHORIZED.value(), "用户认证失败");
        WebUtils.printString(response, JSON.toJSONString(resultData));
    }

}
